﻿@page "/Events/AddPointOnClick"
@using LiveChartsCore.Defaults;
@using LiveChartsCore.Drawing;
@using LiveChartsCore.SkiaSharpView
@using LiveChartsCore.SkiaSharpView.Blazor;
@using LiveChartsCore;
@using System.Collections.ObjectModel;

<CartesianChart
    @ref="_chart"
    Series="@series"
    OnPointerDownCallback="OnPointerDown"
    TooltipPosition="LiveChartsCore.Measure.TooltipPosition.Hidden">
</CartesianChart>

@code {
    private CartesianChart _chart;
    private ObservableCollection<ObservablePoint> _data = new ObservableCollection<ObservablePoint>
    {
        new ObservablePoint(2, 3),
        new ObservablePoint(4, 5),
        new ObservablePoint(6, 2)
    };
    private ISeries[] series;

    protected override void OnInitialized()
    {
        series = new ISeries[]
        {
            new LineSeries<ObservablePoint>
            {
                Values = _data,
                GeometrySize = 18,
                DataPadding = new LiveChartsCore.Drawing.LvcPoint(5,5)
            }
        };
    }

    private void OnPointerDown(PointerEventArgs e)
    {
        var p = new LvcPointD(e.OffsetX, e.OffsetY);
        var dataCoordinates = _chart.ScalePixelsToData(p);
        _data.Add(new ObservablePoint(dataCoordinates.X, dataCoordinates.Y));
    }
}
